/**
 * yvanui.core
 * @author luoyifan
 * 2019-07-07 21:52:00
 */
"use strict";
(function () {

  var idSeq = 0;

  // 取消jQuery缓存
  $.ajaxSetup({
    cache: false
  });

  $.yvan = $.yvan || {};

  $.fn.extend({
    up: function (component) {
      return this.closest("[yvan='" + component + "']");
    },
    down: function (component) {
      return this.find("[yvan='" + component + "']");
    }
  });

  $.extend($.yvan, {
    version: '2.0.0',
    component: {},
    getItems: function (option) {
      var items = [];
      if ('items' in option) {
        if ($.type(option.items) === 'array')
          return option.items;
        else
          return [option.items];
      }
      return items;
    },

    beforeRender: function ($targetDOM, context, option) {
      if ($.type(option.onBeforeRender) === 'function') {
        if (option.onBeforeRender.apply(this, arguments) === false) {
          return false;
        }
      }
      return true;
    },

    afterRender: function ($targetDOM, context, option) {
      if ($.type(option.onRender) === 'function') {
        option.onRender.apply(this, arguments);
      }
    },

    showDialog: function (context, options) {

    },

    confirm: function (title, fn) {
      var id = $.yvan.createId('window');
      var btnId = $.yvan.createId('okc');
      var $dom = $(
        '<div yvan="alert" id="' + id + '" class="yvan-anim yvan-anim-00" style="display: none">' +
        '  <div>询问</div>' +
        '  <div>\n' +
        '    <pre class="c"></pre>\n' +
        '    <div style="float: right; margin-top: 15px;">\n' +
        '      <input type="button" id="ok_' + btnId + '" value="确认" style="margin-right: 10px" />\n' +
        '      <input type="button" id="cancel_' + btnId + '" value="取消" />\n' +
        '    </div>\n' +
        '  </div>\n' +
        '</div>');
      $dom.find('.c').html(title);
      $('body').append($dom);
      var $btnCancel = $('#cancel_' + btnId).jqxButton({ width: '65px' })
        .on('click', fn.no);
      var $btnOk = $('#ok_' + btnId).jqxButton({ width: '65px' })
        .on('click', fn.yes);

      $dom.jqxWindow({
        width: 300,
        animationType: 'none',
        autoOpen: true,
        collapsed: false,
        closeButtonAction: 'close',
        isModal: true,
        showCloseButton: false,
        okButton: $btnOk,
        cancelButton: $btnCancel,
        initContent: function () {
          setTimeout(function () {
            $btnOk.focus();
          });
        }
      }).show();
    },

    alert: function (m) {
      var id = $.yvan.createId('window');
      var btnId = $.yvan.createId('okc');
      var $dom = $(
        '<div yvan="alert" id="' + id + '" class="yvan-anim yvan-anim-00" style="display: none">' +
        '  <div>提示</div>' +
        '  <div>\n' +
        '    <pre class="c"></pre>\n' +
        '    <div style="text-align: center; margin-top: 15px;">\n' +
        '      <input type="button" id="ok_' + btnId + '" value="确定" style="margin-right: 10px" />\n' +
        '    </div>\n' +
        '  </div>\n' +
        '</div>');
      $dom.find('.c').html(m);
      $('body').append($dom);
      var $btnOk = $('#ok_' + btnId).jqxButton({ width: '65px' });

      $dom.jqxWindow({
        width: 300,
        //showAnimationDuration: 200,
        //closeAnimationDuration: 0,
        animationType: 'none',
        autoOpen: true,
        collapsed: false,
        closeButtonAction: 'close',
        isModal: true,
        showCloseButton: false,
        okButton: $btnOk,
        cancelButton: $btnOk,
        initContent: function () {
          setTimeout(function () {
            $btnOk.focus();
          });
        }
      }).show();
    },

    msg: function (m) {
      $('body').down('msg').remove();

      var $w = $(
        //'<div xtype="msg" class="layui-layer layui-layer-dialog layui-layer-border layui-layer-msg layui-layer-hui yvan-anim yvan-anim-00" style="z-index: 10000;">' +
        '<div yvan="msg" class="yvan-msg yvan-anim yvan-anim-00">' +
        '  <div class="yvan-msg-content">' + m + '</div>' +
        '</div>');
      $('body').append($w);

      var iframeWidth = $w.parent().width();
      var iframeHeight = $w.parent().height();

      var windowWidth = $w.width();
      var windowHeight = $w.height();

      var setWidth = (iframeWidth - windowWidth) / 2;
      var setHeight = (iframeHeight - windowHeight) / 2;
      if (iframeHeight < windowHeight || setHeight < 0) {
        setHeight = 0;
      }
      if (iframeWidth < windowWidth || setWidth < 0) {
        setWidth = 0;
      }
      $w.css({ left: setWidth, top: setHeight });
      setTimeout(function () { $w.remove(); }, 3000);
    },

    createId: function (prefix) {
      return (prefix || 'd') + '_' + (idSeq++);
    },

    render: function ($targetDom, context, options) {
      if ($.type(options) === 'function') {
        options = options.call($.yvan, context);
      }

      if ($.type(options) === 'array') {
        options.forEach(function (item) {
          $.yvan.render($targetDom, context, item);
        });
        return;
      }

      if ($.type(options["component"]) === 'string') {
        if (options["component"] in $.yvan.component) {
          $.yvan.component[options["component"]].call($.yvan, $targetDom, context, options);

        } else {
          console.error('component "' + options["component"] + '" not define', options);
        }

      } else {
        console.error('not contains component property', options);
      }
    }
  });

})(jQuery);